//新建
exports.createUser = async (cloud, mixNick, params) => {
    try{
        console.log('mixNick--------------', mixNick);
        console.log('params-----------', params);

        let create_sql = 'insert into el_user (mixnick, nick, chance) values (?, ?, ?)';
        let create_info = await cloud.dataspace.executeSql(create_sql, [mixNick, params.nick, 1]);

        return {
            status: "success",
            data: create_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
};

//根据mixnick查询
exports.findUserByMixNick = async (cloud, mixNick) => {
    try{
        let select_sql = 'select * from el_user where mixnick = ?';
        let select_info = await cloud.dataspace.executeSql(select_sql, [mixNick]);

        return {
            status: "success",
            data: select_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
};

//根据id查询
exports.findUserByUserId = async (cloud, params) => {
    try{
        let select_sql = 'select * from el_user where id = ?';
        let select_info = await cloud.dataspace.executeSql(select_sql, [params.user_id]);

        return {
            status: "success",
            data: select_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
};

//根据mixnick 和 id 查询用户信息
exports.findUserByIdAndMixnick = async (cloud, params, mixNick) => {
    try{
        let select_sql = 'select * from el_user where id = ? and mixnick = ?';
        let select_info = await cloud.dataspace.executeSql(select_sql, [params.user_id, mixNick]);

        return {
            status: "success",
            data: select_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
}

//减少机会
exports.reduceUserChance = async (cloud, params) => {
    try{
        let update_sql = 'update el_user set chance = ? where user_id = ? and chance > 0';
        let update_info = await cloud.dataspace.executeSql(update_sql, [0, params.user_id]);

        return {
            status: "success",
            data: update_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
};

//批量更新机会
exports.addUserChance = async (cloud) => {
    try{
        let update_sql = 'update el_user set chance = ? where chance = ? ';
        let update_info = await cloud.dataspace.executeSql(update_sql, [1, 0]);

        return {
            status: "success",
            data: update_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
};

// 埋点
exports.addUserStat = async (params, time, cloud) => {
	try {
		console.log("==============try==================")
		let query = `insert into tracking_message_record (data, gmt_create, gmt_modified) VALUES ('${params}', '${time}', now())`;
		let data = await cloud.dataspace.executeSql( query);
		return data;
	} catch (err) {
		console.log("==============err==================", err)
		return err;
	}
};